package com.bjdd.jdy.ui.orderDetail;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.model.inner.GeoPoint;
import com.baidu.mapapi.utils.DistanceUtil;
import com.bjdd.jdy.Constant;
import com.bjdd.jdy.R;
import com.bjdd.jdy.baidu.NaviUtils;
import com.bjdd.jdy.base.BaseActivity;
import com.bjdd.jdy.base.BaseResult;
import com.bjdd.jdy.base.RxManager;
import com.bjdd.jdy.entity.OrderBean;
import com.bjdd.jdy.entity.RoutePoint;
import com.bjdd.jdy.ui.map.MapActivity;
import com.bjdd.jdy.util.JumpUtil;
import com.bjdd.jdy.util.L;
import com.bjdd.jdy.util.StatusBarUtil;
import com.bjdd.jdy.util.T;
import com.zhy.autolayout.AutoLinearLayout;
import com.zhy.autolayout.AutoRelativeLayout;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class OrderDetailActivity extends BaseActivity<OrderDetailPresenter, OrderDetailModel> implements OrderDetailContract.View {

    @Bind(R.id.toolbar_back)
    ImageView toolbarBack;
    @Bind(R.id.title)
    TextView title;
    @Bind(R.id.tv_toolbar_right)
    TextView tvToolbarRight;
    @Bind(R.id.toolbar)
    AutoRelativeLayout toolbar;
    @Bind(R.id.tv_order_send_name)
    TextView tvSendName;
    @Bind(R.id.tv_order_send_mobile)
    TextView tvSendMobile;
    @Bind(R.id.tv_order_send_address)
    TextView tvSendAddress;
    @Bind(R.id.tv_order_receive_name)
    TextView tvReceiveName;
    @Bind(R.id.tv_order_receive_mobile)
    TextView tvReceiveMobile;
    @Bind(R.id.tv_order_receive_address)
    TextView tvReceiveAddress;
    @Bind(R.id.tv_object_money)
    TextView tvObjectMoney;
    @Bind(R.id.tv_object_type)
    TextView tvObjectType;
    @Bind(R.id.tv_object_size)
    TextView tvObjectSize;
    @Bind(R.id.tv_order_no)
    TextView tvOrderNo;
    @Bind(R.id.tv_order_state)
    TextView tvOrderState;
    /*@Bind(R.id.tv_order_detail)
    TextView tvOrderDetail;*/
    @Bind(R.id.tv_order_money)
    TextView tvOrderMoney;
    @Bind(R.id.btn_take)
    Button btnTake;
    @Bind(R.id.btn_map)
    Button btnMap;
    @Bind(R.id.all_orderdetail_content)
    AutoLinearLayout mainContent;
    private OrderBean orderBean;
    private Activity activity;
    private RxManager rxManager;

    @Override
    public int getLayoutId() {
        return R.layout.activity_order_detail;
    }
    @Override
    public void initView() {
        StatusBarUtil.setColor(this, Color.parseColor("#F9F9F9"), 0);
        title.setText("订单详情");
        tvToolbarRight.setText("异常申报");
        tvToolbarRight.setVisibility(View.VISIBLE);
        Bundle bundle = getIntent().getExtras();
        String orderNo = bundle.getString("orderNo");
        presenter.getOrderInfo(orderNo);
        activity = this;
        rxManager = new RxManager();
        rxManager.on(Constant.INSPECTION_FINISH,msg -> inspectionFinish());
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @OnClick({R.id.toolbar_back, R.id.tv_toolbar_right, R.id.btn_take, R.id.btn_map,R.id.img_call_receive,R.id.img_call_send})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.toolbar_back:
                finish();
                break;
            case R.id.tv_toolbar_right:
                JumpUtil.toCall(context,Constant.CALL_PHONE);
                break;
            case R.id.btn_take:
                switch (orderBean.getOrderStatus()){
                    case Constant.ORDER_PAID:

                        break;
                    case Constant.ORDER_PAIR:
                        JumpUtil.toInspection(context,orderBean.getOrderNo());
                        break;
                    case Constant.ORDER_SENDING:
                        if (orderBean.getPayType() == Constant.TO_PAY){
                            JumpUtil.toPay(context,orderBean.getOrderNo(),orderBean.getReceiverLat(),orderBean.getReceiverLng());
                        }else {
                            JumpUtil.toSignScan(context,orderBean.getOrderNo()+"",orderBean.getOrderId()+"",orderBean.getReceiverLat(),orderBean.getReceiverLng());
                            //JumpUtil.toSign(context,orderBean.getOrderNo());
                        }
                        break;
                }
                break;
            case R.id.btn_map:
                RoutePoint point = new RoutePoint();
                point.setStartLat(orderBean.getConsignerLat());
                point.setStartLng(orderBean.getConsignerLng());
                point.setEndLat(orderBean.getReceiverLat());
                point.setEndLng(orderBean.getReceiverLng());
                switch (orderBean.getOrderStatus()){
                    case Constant.ORDER_PAID:
                        Intent intent = new Intent(context, MapActivity.class);
                        Bundle bundle = new Bundle();
                        bundle.putSerializable(Constant.POINTS,point);
                        intent.putExtras(bundle);
                        context.startActivity(intent);
                        break;
                    case Constant.ORDER_PAIR:
                        point.setEndLng(orderBean.getConsignerLng());
                        point.setEndLat(orderBean.getConsignerLat());
                        L.d("坐标："+point.toString());
                        NaviUtils.getInstance(activity).start(point);
                        break;
                    case Constant.ORDER_SENDING:
                        NaviUtils.getInstance(activity).start(point);
                        break;
                }
                break;
            case R.id.img_call_receive:
                JumpUtil.toCall(context,orderBean.getReceiverMobile());
                break;
            case R.id.img_call_send:
                JumpUtil.toCall(context,orderBean.getConsignerMobile());
                break;
        }
    }

    @Override
    public void infoBack(OrderBean order) {
        if (order == null){
            T.show("获取订单为空");
            return;
        }
        orderBean = order;
        tvSendName.setText(order.getConsignerName());
        tvSendMobile.setText(order.getConsignerMobile());
        tvSendAddress.setText(order.getConsignerAddress());
        tvReceiveName.setText(order.getReceiverName());
        tvReceiveMobile.setText(order.getReceiverMobile());
        tvReceiveAddress.setText(order.getReceiverAddress());
        tvObjectMoney.setText("￥"+order.getObjectValue());
        tvObjectType.setText(order.getItemName());
        tvObjectSize.setText(order.getObjectWeight()+" kg");
        tvOrderNo.setText(order.getOrderNo());
        switch (order.getOrderStatus()){
            case Constant.ORDER_PAIR:
                //待取货
                tvOrderState.setText("待取货");
                btnTake.setText("确认取货");
                break;
            case Constant.ORDER_SENDING:
                //正在配送
                tvOrderState.setText("待送货");
                btnTake.setText("确认收货");
                break;
            case Constant.ORDER_CANCEL:
                //订单取消
                tvOrderState.setText("订单取消");
                btnTake.setVisibility(View.GONE);
                btnMap.setVisibility(View.GONE);
                break;
            case Constant.ORDER_FINISH:
                //订单完成
                tvOrderState.setText("订单完成");
                btnTake.setVisibility(View.GONE);
                btnMap.setVisibility(View.GONE);
                break;
            case Constant.ORDER_ERROR:
                //订单异常
                tvOrderState.setText("订单异常");
                btnTake.setVisibility(View.GONE);
                btnMap.setVisibility(View.GONE);
                break;
        }
        tvOrderMoney.setText("￥"+order.getCarrierShowMoney());
        mainContent.setVisibility(View.VISIBLE);
    }


    private void inspectionFinish(){
        finish();
    }


}
